/*
   Copyright (C) 2006
   Andrew Madigan

This file is part of nhttpd.

nhttpd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

nhttpd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with nhttpd; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

#ifndef SERVLET_H
#define SERVLET_H
#include <nhttpd/reader.h>
#include <nhttpd/hashtable.h>
#include <nhttpd/request.h>
#include <nhttpd/response.h>
#include <nhttpd/file.h>

typedef struct	{
	void* priv;
	//Map<char*,void*>
	Hashtable* attributes;
	char* contextPath;
	//Map<char*,char*>
	Hashtable* initParams;
	int majorVersion;
	int minorVersion;
	char* serverInfo;
	char* name;
} ServletContext;

//NOTE: While the create method in each module is allowed to create the Servlet
//object, to add more data to the struct nested structs should be used to avoid
//breaking the API.
typedef struct Servlet	{
	void* priv; //Private data stored by the container
	//Map<char*,List<char*>>
	Hashtable* initParams;
	char* name;
	ServletContext* context;
	
	//These values are to be filled in by the application
	char* info;	
	void (*Init)(struct Servlet* restrict self);
	void (*Service)(struct Servlet* restrict self, 
							HTTP_Request* restrict request, 
							HTTP_Response* restrict response);
	void (*Free)(struct Servlet* restrict self);
} Servlet;

void SR_Log(const Servlet* restrict self, const char* restrict format, ...);

//The symbol matching this in each library is expected to be called 
//"CreateServlet" by default, though this is set in the configuration
typedef Servlet* (*CreateServletFunc) ();

ServletContext* SC_GetContext(const ServletContext* restrict self, 
										const char* restrict uripath);

void SC_ForwardByName(const ServletContext* restrict self, 
							 HTTP_Request* restrict req, 
							 HTTP_Response* restrict resp, const char* restrict name);

void SC_IncludeByName(const ServletContext* restrict self, 
							 HTTP_Request* restrict req, 
							 HTTP_Response* restrict resp, const char* restrict name);

void SC_ForwardByPath(const ServletContext* restrict self, 
							 HTTP_Request* restrict req, 
							 HTTP_Response* restrict resp, const char* restrict path);
							 
void SC_IncludeByPath(const ServletContext* restrict self, 
							 HTTP_Request* restrict req, 
							 HTTP_Response* restrict resp, const char* restrict path);
							 
char* SC_GetRealPath(const ServletContext* restrict self, const char* path);

char* SC_GetResourceURL(const ServletContext* restrict self, 
								const char* restrict path);

NHFile* SC_GetFile(const ServletContext* restrict self, 
						 const char* restrict path);

void SC_Log(const ServletContext* restrict self, 
				const char* restrict format, ...);

#endif
